/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jboss.seam.example.booking.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.example.booking.entity.User;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.Controller;
import org.jboss.seam.international.StatusMessage.Severity;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;

/**
 *
 * @author RHO
 */
@AutoCreate
@Name("changePasswordPage")
@Scope(ScopeType.EVENT)
@Restrict("#{identity.loggedIn}")
public class ChangePasswordPage extends Controller {

    private String oldPasswd;
    private String newPasswd;
    private String newPasswdRepeat;
    @In
    private EntityManager entityManager;
    @In
    private User user;
    @In
    private JbpmContext jbpmContext;

    public String changePassword() {
        System.out.println("oldPasswd:" + oldPasswd);
        System.out.println("newPasswd:" + newPasswd);
        System.out.println("newPasswdRepeat:" + newPasswdRepeat);
        try {
            ProcessInstance processInstance =
                    jbpmContext.newProcessInstance("password");

            //Create variables
            Map<String, String> processVar = new HashMap<String, String>();
            processVar.put("oldPasswd", oldPasswd);
            processVar.put("newPasswd", newPasswd);
            processVar.put("newPasswdRepeat", newPasswdRepeat);

            processInstance.getContextInstance().addVariables(processVar);
            processInstance.setStart(new Date());
            processInstance.signal();
            jbpmContext.save(processInstance);

        } catch (Exception exc) {
        }
        
        FacesMessages.instance().addFromResourceBundle(Severity.INFO,
                "Password changed");
        
        return "show_change_password";
    }

    public String getNewPasswd() {
        return newPasswd;
    }

    public void setNewPasswd(String newPasswd) {
        this.newPasswd = newPasswd;
    }

    public String getNewPasswdRepeat() {
        return newPasswdRepeat;
    }

    public void setNewPasswdRepeat(String newPasswdRepeat) {
        this.newPasswdRepeat = newPasswdRepeat;
    }

    public String getOldPasswd() {
        return oldPasswd;
    }

    public void setOldPasswd(String oldPasswd) {
        this.oldPasswd = oldPasswd;
    }
}
